package FileChange2;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;

public class MainPanel extends JFrame implements ActionListener{
	
	private JTextField pnA_text;
	private JTextField pnB_text;
	private Zuoye_change move;
	MainPanel(){
		move = new Zuoye_change();
		JPanel pnA = new JPanel();
		JPanel pnB = new JPanel();
		JPanel pnC = new JPanel();
		JButton btTurn = new JButton("Turn");
		btTurn.addActionListener(this);
		
		//面板pnA
		JLabel pnA_lbA = new JLabel("文件夹A");
		JButton pnA_btSelect = new JButton("selectA");
		pnA_btSelect.addActionListener(this);
		pnA_text = new JTextField("                                                  ");
		pnA_text.setEnabled(false);
		pnA.add(pnA_lbA);
		pnA.add(pnA_text);
		pnA.add(pnA_btSelect);
		
		//面板pnB
		JLabel pnB_lbB = new JLabel("文件夹B");
		JButton pnB_btSelect = new JButton("selectB");
		pnB_btSelect.addActionListener(this);
		pnB_text = new JTextField("                                                     ");
		pnB_text.setEnabled(false);
		pnB.add(pnB_lbB);
		pnB.add(pnB_text);
		pnB.add(pnB_btSelect);
		
		//面板pnC
		pnC.add(btTurn);
		
		getContentPane().setLayout(new GridLayout(3,1));
		getContentPane().add(pnA);
		getContentPane().add(pnB);
		getContentPane().add(pnC);
		
		setSize(400,200);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		new MainPanel();
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("Turn")){
			if(pnA_text.getText().trim().length()==0||pnB_text.getText().trim().length()==0){
				JOptionPane.showMessageDialog(null, "请选择两个文件夹");
				return;
			}
			if(pnA_text.getText().trim().equals(pnB_text.getText().trim())){
				JOptionPane.showMessageDialog(null, "您选择的文件夹为\n同一个文件夹");
				return;
			}
			
			move.change(new File(pnA_text.getText().trim()),new File(pnB_text.getText().trim()));
			return;
		}
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(chooser.DIRECTORIES_ONLY);
		if(JFileChooser.CANCEL_OPTION==chooser.showDialog(null, "选择"))
			return;
		File selectFile = chooser.getSelectedFile();
		String pathSelect = selectFile.getAbsolutePath();
		if(e.getActionCommand().equals("selectA"))
			pnA_text.setText(pathSelect);
		else
			pnB_text.setText(pathSelect);
			
		
	}

}
